package ruifeng.zcj.happymahjony.application;

import android.app.Application;

import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

import org.apache.log4j.Level;

import de.mindpipe.android.logging.log4j.LogConfigurator;
import ruifeng.zcj.happymahjony.constant.KeyConstant;
import ruifeng.zcj.happymahjony.handle.CrashHandler;
import ruifeng.zcj.happymahjony.util.CacheUtil;
import ruifeng.zcj.happymahjony.util.Log;

/**
 * 作者：郑朝军 on 2016/11/8 10:50
 * 邮箱：1250393285@qq.com
 * 公司：武汉润丰壹网信息科技有限公司
 */

public class App extends Application {

    private static App instance;

    private IWXAPI mWXAPI;

    private boolean isLoginSuccess = false;

    public void onCreate() {
        super.onCreate();
        instance = this;
        CacheUtil.setCacheClient(new CacheUtil(getInstance()));
        initSDK();
        initLog4j();
        initCrashHandler();
    }

    public static App getInstance() {
        return instance;
    }

    private void initSDK() {
        mWXAPI = WXAPIFactory.createWXAPI(this, KeyConstant.APP_ID_WX, true);
        mWXAPI.registerApp(KeyConstant.APP_ID_WX);
    }

    public IWXAPI getWXAPI() {
        return mWXAPI;
    }

    public boolean isLoginSuccess() {
        return isLoginSuccess;
    }

    public void setLoginSuccess(boolean isLoginSuccess) {
        this.isLoginSuccess = isLoginSuccess;
    }

    /**
     * 初始化错误收集器
     */
    private void initCrashHandler() {
        CrashHandler crashHandler = CrashHandler.getInstance();
        crashHandler.init(this);
        crashHandler.sendPreviousReportsToServer();
    }

    private void initLog4j() {
        String path = Log.getLogPath(this);
        LogConfigurator logConfigurator = new LogConfigurator();
        logConfigurator.setFileName(path);
        logConfigurator.setRootLevel(Level.DEBUG);
        logConfigurator.setLevel("org.apache", Level.ERROR);
        logConfigurator.setFilePattern("%d %-5p [%c{2}]-[%L] %m%n");
        logConfigurator.setMaxFileSize(1024 * 1024 * 5);
        logConfigurator.setImmediateFlush(true);
        logConfigurator.configure();

        Log.notifyDataSetChanged(this);

    }
}
